{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "663c4189",
   "metadata": {},
   "source": [
    "# week10课堂笔记\n",
    "* 2023-9-18\n",
    "* 课程：python语言\n",
    "* week03\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3388b0df",
   "metadata": {},
   "source": [
    "## 1.元组\n",
    "> 1. 概念\n",
    "> 2. 创建\n",
    "> 3. 一般方法\n",
    "## 2.集合\n",
    "> 1. 概念\n",
    "> 2. 创建\n",
    "> 3. 一般方法\n",
    "## 3. 字典\n",
    "> 1. 概念\n",
    "> 2. 创建\n",
    "> 3. 一般方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cdd06b79",
   "metadata": {},
   "source": [
    "[pynative学习网站推荐](https://pynative.com/)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5f55881",
   "metadata": {},
   "source": [
    "## 1.元组"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31d438c6",
   "metadata": {},
   "source": [
    "### 1.1元组的创建方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "60e56e32",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1699873495.5184283"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import time\n",
    "time.time()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a38a4fa8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.第一种创建方式\n",
    "my_tuplel = 1699873495,100001,\"小明\",18,\"冰岛\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f52108a6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1699873495, 100001, '小明', 18, '冰岛')\n",
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "print(my_tuplel)\n",
    "print(type(my_tuplel))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8ee75f10",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1699873495, 100001, '小明', 18, '冰岛')\n",
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "my_tuplel2 = (1699873495,100001,\"小明\",18,\"冰岛\")\n",
    "print(my_tuplel2)\n",
    "print(type(my_tuplel2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b39c553e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('Jessa', 30, 45.75, [25, 78])\n",
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "# 2.第二种创建方法\n",
    "my_tuplel3= tuple(('Jessa', 30, 45.75, [25, 78]))\n",
    "print(my_tuplel3)\n",
    "print(type(my_tuplel3))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2ff0127",
   "metadata": {},
   "source": [
    "### 1.2 元组的一般方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "444cc1ed",
   "metadata": {},
   "source": [
    "* 1. 关于序列数据的一般方法均适用于元组\n",
    "> 1. indexing and slicing(索取取值和切片)\n",
    "> 2. in方法\n",
    "> 3. len方法\n",
    "> 4. for循环遍历\n",
    "* 2.元组独有的一些常用方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "994623af",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "45.75"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_tuplel3[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "88c7483f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(45.75, [25, 78])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_tuplel3[2:4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "a7169746",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'Jessa' in  my_tuplel3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "578b68a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'Jesxa' in  my_tuplel3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "e57b3a9b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(my_tuplel3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "c4bcf6ae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jessa\n",
      "30\n",
      "45.75\n",
      "[25, 78]\n"
     ]
    }
   ],
   "source": [
    "for i in my_tuplel3:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "70f875cb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_tuplel3.count(45.75)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "78f6597b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_tuplel3.index([25, 78])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "a78f1aa9",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[19], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m my_tuplel3[\u001b[38;5;241m0\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mJesxa\u001b[39m\u001b[38;5;124m'\u001b[39m\n",
      "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "my_tuplel3[0] = 'Jesxa'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c337b0d",
   "metadata": {},
   "source": [
    "## 2.集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "8d93488f",
   "metadata": {},
   "outputs": [],
   "source": [
    "歌剧社 = {'小明','小红','李华','张三'}\n",
    "街舞社 = {'小明','李鹏','王芳'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "53c28ca2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'小明', '小红', '张三', '李华', '李鹏', '王芳'}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 并集：去重\n",
    "歌剧社.union(街舞社)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "24b4ad9c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'小明', '小红', '张三', '李华', '李鹏', '王芳'}"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "歌剧社 | 街舞社"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "6da756d9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'小明'}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 交集：找出重复项\n",
    "歌剧社.intersection(街舞社)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "ae0cc0a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'小明'}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "歌剧社 & 街舞社"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "b7d72348",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'小红', '张三', '李华'}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 差集\n",
    "歌剧社.difference(街舞社)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "19ce1a63",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'小红', '张三', '李华'}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "歌剧社 - 街舞社"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "22bb4c94",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'李鹏', '王芳'}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "街舞社.difference(歌剧社)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "54dba66d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'小红', '张三', '李华', '李鹏', '王芳'}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对称差集\n",
    "歌剧社.symmetric_difference(街舞社)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "6c204420",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function symmetric_difference:\n",
      "\n",
      "symmetric_difference(...) method of builtins.set instance\n",
      "    Return the symmetric difference of two sets as a new set.\n",
      "    \n",
      "    (i.e. all elements that are in exactly one of the sets.)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(歌剧社.symmetric_difference)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c8930fa",
   "metadata": {},
   "source": [
    "## 3. 字典dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "bb02c6f5",
   "metadata": {},
   "outputs": [],
   "source": [
    "info = {\n",
    "    \"stu2201\":\"ziqi\",\n",
    "    \"stu2202\":\"xiaofeng\",\n",
    "    \"stu2203\":\"liuyu\",\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "017a50fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu2201': 'ziqi', 'stu2202': 'xiaofeng', 'stu2203': 'liuyu'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "print(info)\n",
    "print(type(info))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "d2aaeabc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'xiaofeng'"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info[\"stu2202\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "0c126bab",
   "metadata": {},
   "outputs": [],
   "source": [
    "info_01 = {\n",
    "    \"stu2201\":[\"ziqi\",2200001,18,\"女\"],\n",
    "    \"stu2202\":[\"xiaofeng\",2200002,0,\"男\"],\n",
    "    \"stu2203\":[\"liuyu\",2200003,19,\"\"],\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d6b9b961",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
